package com.storyroad.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.storyroad.utils.DBConnection;
import com.storyroad.utils.TableNames;

/**
 * Servlet implementation class ChangePicture
 */
public class ChangePicture extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ChangePicture() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * Changes the profile picture of a registered user. Imgur's API is used for online hosting of uploaded images.  
	 * @param request HttpServletRequest object that contains data coming from front-end
	 * @param response HttpServletResponse object that contains data which is prepared after processing the incoming data
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		String username;
		String f_photoLink;
		PreparedStatement stmt = null;
		Connection conn = new DBConnection().connect();
		PrintWriter out = response.getWriter();

		String f_requestType = request.getParameter("request_type");
		if (f_requestType == null || !f_requestType.equals("Android")) {
			f_requestType = "Web";
		}
		if (conn == null) {
			out.println("Connection error");
		} else {
			try {
				if (f_requestType.equals("Web")) {
					username = (String) request.getSession().getAttribute("username");
				} else {
					username = request.getParameter("username");
				}
				f_photoLink = request.getParameter("photo_link");
				/*
				 * Imgur's API is used to host the uploaded the images online. Link to the uploaded image (given by the API)
				 * is stored in the database.
				 */
				
				stmt = conn.prepareStatement("UPDATE " + TableNames.USERS_TABLE
						+ " SET photoLink = ? WHERE username = ?");
				stmt.setString(1, f_photoLink);
				stmt.setString(2, username);
				stmt.executeUpdate();
				conn.close();
				// After the operation is completed, the users is redirected to his profile page. 
				request.getRequestDispatcher("GetProfile").forward(request,	response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				out.print(e.getMessage());
				e.printStackTrace();
			}
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

}
